import Vue from 'vue'
import Router from 'vue-router'
// 页面已运行就会立即加载这个组件
// import login from '@/views/login/index'
// 引入多个动态路由模块
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

Vue.use(Router)

/* Layout */
// 首页的布局组件
import Layout from '@/layout'

// 静态路由:
// 所有用户都可以访问的页面
export const constantRoutes = [
// 登录
  {
    path: '/login',
    // component: login,
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 404
  {
    path: '/404',
    // import('')加载组件是当访问这个路由的时候才会加载组件
    component: () => import('@/views/404'),
    hidden: true
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  {
    path: '/lifeCir',
    name: 'LifeCir',
    component: () => import('../../demo/lifeCir.vue'),
    hidden: true
  },
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import')
    }]

  }

]
// 动态路由:
// 根据当前用户权限访问的页面
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  /*
    提变量的原因
    1.路由规则 太多了，写在Router 显得代码特别臃肿
    2.导出路由规则，方便其他外部文件可以使用这个路由规则
    */
  //  展开拼接动态路由和静态路由
  routes: [...constantRoutes, ...asyncRoutes, { path: '*', redirect: '/404', hidden: true }]
})

// 可以在项目通过这个方法拿到过个路由对象
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
